<?php
/**
 * +--------------------------------------------------------+
 * @Created by Phpstorm.
 * @Category PermissionRepository.php
 * @Depiction
 * @Author hwy <429208948@qq.com>
 * @Date 2022/4/7  16:56
 * +--------------------------------------------------------+
 */

namespace App\Repositories;

use App\Models\Permission;

class PermissionRepository extends BaseRepository
{
    protected $permission;

    public function __construct(Permission $permission)
    {
        $this->permission = $permission;
    }

    /**
     * @param $id
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/8  17:52
     */
    public function getInfo($id)
    {
        return $this->permission->query()->find($id);
    }

    /**
     * @param $ids
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/8  17:47
     */
    public function getAllInId($ids)
    {
        return $data = $this->permission->query()->whereIn('id', $ids)->get();
    }

    /**
     * @param $params
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/8  17:47
     */
    public function getList($params)
    {
        $query = $this->permission->query();

        $perPage = $this->perPage($params);
        [$sort, $order] = $this->paramsSet($params);

        $query = $query->where('guard_name', $params['guard_name']);

        if (!empty($params['name'])) {
            $query = $query->where('name', 'like', '%' . $params['name'] . '%');
        }
        if (!empty($params['title'])) {
            $query = $query->where('title', 'like', '%' . $params['title'] . '%');
        }
        if (!empty($params['path'])) {
            $query = $query->where('path', 'like', '%' . $params['path'] . '%');
        }
        if (!empty($params['start_at'])) {
            $query = $query->where('created_at', [$params['start_at'], $params['end_at']]);
        }

        return $list = $query->select('id', 'pid', 'name', 'title', 'icon', 'path', 'component',
            'guard_name', 'sort', 'hidden', 'created_at', 'updated_at')
            ->orderBy($sort, $order)
            ->paginate($perPage);
    }

    /**
     * @param $params
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/8  17:47
     */
    public function getTree($params)
    {
        $query = $this->permission->query();

        $query = $query->where('guard_name', $params['guard_name']);
        return $list = $query->select('id', 'pid', 'name', 'title', 'icon')
            ->orderByDesc('sort')
            ->get();
    }

    /**
     * @param $params
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model
     * @Depiction 创建
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/11  11:48
     */
    public function create($params)
    {
        return $this->permission->query()->create($params);

    }

    /**
     * @param $id
     * @param $update
     * @return int
     * @Depiction 更新
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/11  11:48
     */
    public function update($id, $update)
    {
        $query = $this->permission->query();

        return $res = $query->where('id', $id)->update($update);
    }

    /**
     * @param $permission
     * @param $update
     * @return mixed
     * @Depiction 更新
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/11  11:49
     */
    public function updateByModel($permission, $update)
    {
        return $permission->update($update);
    }


    /**
     * @param $permission
     * @return mixed
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/11  11:50
     */
    public function delete($permission)
    {
        return $permission->delete();
    }

}
